home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / source.exe / POSIX / LSTLIB / LSTCON~1.C < prev    next >
C/C++ Source or Header  |  1993-08-15  |  7KB  |  175 lines

  1. /*
  2.  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Adam de Boor.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)lstConcat.c    5.3 (Berkeley) 6/1/90";
  39. #endif /* not lint */
  40.  
  41. /*-
  42.  * listConcat.c --
  43.  *    Function to concatentate two lists.
  44.  */
  45.  
  46. #include    "lstInt.h"
  47.  
  48. /*-
  49.  *-----------------------------------------------------------------------
  50.  * Lst_Concat --
  51.  *    Concatenate two lists. New elements are created to hold the data
  52.  *    elements, if specified, but the elements themselves are not copied.
  53.  *    If the elements should be duplicated to avoid confusion with another
  54.  *    list, the Lst_Duplicate function should be called first.
  55.  *    If LST_CONCLINK is specified, the second list is destroyed since
  56.  *    its pointers have been corrupted and the list is no longer useable.
  57.  *
  58.  * Results:
  59.  *    SUCCESS if all went well. FAILURE otherwise.
  60.  *
  61.  * Side Effects:
  62.  *    New elements are created and appended the the first list.
  63.  *-----------------------------------------------------------------------
  64.  */
  65. ReturnStatus
  66. Lst_Concat (Lst l1, Lst l2, int flags)
  67.     /* Lst              l1;     ** The list to which l2 is to be appended */
  68.     /* Lst              l2;     ** The list to append to l1 */
  69.     /* int                 flags;  ** LST_CONCNEW if LstNode's should be duplicated
  70.                  * LST_CONCLINK if should just be relinked */
  71. {
  72.     register ListNode      ln;     /* original LstNode */
  73.     register ListNode      nln;    /* new LstNode */
  74.     register ListNode      last;   /* the last element in the list. Keeps
  75.                  * bookkeeping until the end */
  76.     register List     list1 = (List)l1;
  77.     register List     list2 = (List)l2;
  78.  
  79.     if (!LstValid (l1) || !LstValid (l2)) {
  80.     return (FAILURE);
  81.     }
  82.  
  83.     if (flags == LST_CONCLINK) {
  84.     if (list2->firstPtr != NilListNode) {
  85.         /*
  86.          * We set the nextPtr of the
  87.          * last element of list two to be NIL to make the loop easier and
  88.          * so we don't need an extra case should the first list turn
  89.          * out to be non-circular -- the final element will already point
  90.          * to NIL space and the first element will be untouched if it
  91.          * existed before and will also point to NIL space if it didn't.
  92.          */
  93.         list2->lastPtr->nextPtr = NilListNode;
  94.         /*
  95.          * So long as the second list isn't empty, we just link the
  96.          * first element of the second list to the last element of the
  97.          * first list. If the first list isn't empty, we then link the
  98.          * last element of the list to the first element of the second list
  99.          * The last element of the second list, if it exists, then becomes
  100.          * the last element of the first list.
  101.          */
  102.         list2->firstPtr->prevPtr = list1->lastPtr;
  103.         if (list1->lastPtr != NilListNode) {
  104.          list1->lastPtr->nextPtr = list2->firstPtr;
  105.         }
  106.         list1->lastPtr = list2->lastPtr;
  107.     }
  108.     if (list1->isCirc && list1->firstPtr != NilListNode) {
  109.         /*
  110.          * If the first list is supposed to be circular and it is (now)
  111.          * non-empty, we must make sure it's circular by linking the
  112.          * first element to the last and vice versa
  113.          */
  114.         list1->firstPtr->prevPtr = list1->lastPtr;
  115.         list1->lastPtr->nextPtr = list1->firstPtr;
  116.     }
  117.     free ((Address)l2);
  118.     } else if (list2->firstPtr != NilListNode) {
  119.     /*
  120.      * We set the nextPtr of the last element of list 2 to be nil to make
  121.      * the loop less difficult. The loop simply goes through the entire
  122.      * second list creating new LstNodes and filling in the nextPtr, and
  123.      * prevPtr to fit into l1 and its datum field from the
  124.      * datum field of the corresponding element in l2. The 'last' node
  125.      * follows the last of the new nodes along until the entire l2 has
  126.      * been appended. Only then does the bookkeeping catch up with the
  127.      * changes. During the first iteration of the loop, if 'last' is nil,
  128.      * the first list must have been empty so the newly-created node is
  129.      * made the first node of the list.
  130.      */
  131.     list2->lastPtr->nextPtr = NilListNode;
  132.     for (last = list1->lastPtr, ln = list2->firstPtr;
  133.          ln != NilListNode;
  134.          ln = ln->nextPtr)
  135.     {
  136.         PAlloc (nln, ListNode);
  137.         nln->datum = ln->datum;
  138.         if (last != NilListNode) {
  139.         last->nextPtr = nln;
  140.         } else {
  141.         list1->firstPtr = nln;
  142.         }
  143.         nln->prevPtr = last;
  144.         nln->flags = nln->useCount = 0;
  145.         last = nln;
  146.     }
  147.  
  148.     /*
  149.      * Finish bookkeeping. The last new element becomes the last element
  150.      * of list one. 
  151.      */
  152.     list1->lastPtr = last;
  153.  
  154.     /*
  155.      * The circularity of both list one and list two must be corrected
  156.      * for -- list one because of the new nodes added to it; list two
  157.      * because of the alteration of list2->lastPtr's nextPtr to ease the
  158.      * above for loop.
  159.      */
  160.     if (list1->isCirc) {
  161.         list1->lastPtr->nextPtr = list1->firstPtr;
  162.         list1->firstPtr->prevPtr = list1->lastPtr;
  163.     } else {
  164.         last->nextPtr = NilListNode;
  165.     }
  166.  
  167.     if (list2->isCirc) {
  168.         list2->lastPtr->nextPtr = list2->firstPtr;
  169.     }
  170.     }
  171.  
  172.     return (SUCCESS);
  173. }
  174.     
  175.